﻿using System;
using System.Web;
using com.huigo.Admin.Web.Config;
using System.Web.Caching;
using com.loto.Common;


namespace com.huigo.Admin.Web.config
{
    public class BaseConfig
    {

        public static MenuConfig LoadMenuConfig(string username)
        {
            if (!string.IsNullOrEmpty(username))
            {
                MenuConfig menuConfig = (MenuConfig)HttpContext.Current.Cache[username + "MenuConfig"];
                if (menuConfig != null)
                {
                    return menuConfig;
                }
                string configPath = PathHandle.GetFilePath("~/App_Data/" + username + "MenuConfig.xml");
                try
                {
                    menuConfig = (MenuConfig)com.loto.Common.SerializationHelper.Load(typeof(MenuConfig), configPath);
                    CacheDependency cacheDependency = new CacheDependency(configPath);
                    HttpContext.Current.Cache.Insert(username + "MenuConfig", menuConfig, cacheDependency);
                }
                catch (Exception err)
                {
                    //throw new Exception("载入配置文件：" + configPath + "出错。错误消息：" + err.Message);
                    return null;
                }
                return menuConfig;
            }
            else
                return null;
        }

        public static MenuConfig LoadMenuConfig()
        {
            MenuConfig menuConfig = (MenuConfig)HttpContext.Current.Cache["MenuConfig"];
            if (menuConfig != null)
            {
                return menuConfig;
            }
            string configPath = PathHandle.GetFilePath("~/App_Data/MenuConfig.xml");
            try
            {
                menuConfig = (MenuConfig)com.loto.Common.SerializationHelper.Load(typeof(MenuConfig), configPath);
                CacheDependency cacheDependency = new CacheDependency(configPath);
                HttpContext.Current.Cache.Insert("MenuConfig", menuConfig, cacheDependency);
            }
            catch (Exception err)
            {
                throw new Exception("载入配置文件：" + configPath + "出错。错误消息：" + err.Message);
                return null;
            }
            return menuConfig;
        }

        public static PageRelated LoadPageRelated()
        {
            PageRelated pageRelated = (PageRelated)HttpContext.Current.Cache["PageRelated"];
            if (pageRelated != null)
            {
                return pageRelated;
            }
            string configPath = PathHandle.GetFilePath("~/App_Data/PageRelated.xml");
            try
            {
                pageRelated = (PageRelated)com.loto.Common.SerializationHelper.Load(typeof(PageRelated), configPath);
                CacheDependency cacheDependency = new CacheDependency(configPath);
                HttpContext.Current.Cache.Insert("PageRelated", pageRelated, cacheDependency);
            }
            catch (Exception err)
            {
                //throw new Exception("载入配置文件：" + configPath + "出错。错误消息：" + err.Message);
                return null;
            }
            return pageRelated;
        }


        public static int GetParentMenuId(MenuConfig mc, string url)
        {
            if (mc != null)
            {
                foreach (com.huigo.Admin.Web.Config.Model.Menu ss in mc.MenuList)
                {
                    if (ss.Url.ToLower() == url.ToLower())
                    {
                        return ss.ID;
                    }
                    foreach (com.huigo.Admin.Web.Config.Model.BaseMenu s in ss.ChildMenu)
                    {
                        if (s.Url.ToLower() == url.ToLower())
                        {
                            return s.ParentId;
                        }
                    }
                }
            }
            return int.MinValue;
        }

        public static int GetMenuId(MenuConfig mc, string url)
        {
            if (mc != null)
            {
                foreach (com.huigo.Admin.Web.Config.Model.Menu ss in mc.MenuList)
                {
                    foreach (com.huigo.Admin.Web.Config.Model.BaseMenu s in ss.ChildMenu)
                    {
                        if (s.Url.ToLower() == url.ToLower())
                        {
                            return s.ID;
                        }
                    }
                }
            }
            return int.MinValue;
        }

        public static bool HasPageAccess(string userName)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            string relatedUrl = string.Empty;
            PageRelated pr = LoadPageRelated();
            foreach (com.huigo.Admin.Web.Config.Model.Related rr in pr.PageRelatedList)
            {
                foreach (com.huigo.Admin.Web.Config.Model.BaseRelated r in rr.ChildRelated)
                {
                    if (r.Url.ToLower().Equals(url.ToLower()))
                    {
                        relatedUrl = rr.Url;
                        break;
                    }
                }
            }
            if (relatedUrl != string.Empty)
            {
                MenuConfig mc = LoadMenuConfig(userName);
                int currentParentId = GetParentMenuId(mc, relatedUrl);
                int currentId = GetMenuId(mc, relatedUrl);
                if (currentParentId == int.MinValue && currentId == int.MinValue)
                {
                    return false;
                }
                return true;
            }
            return false;
        }
    }

}
